/*
 *   Swing Explorer. Tool for developers exploring Java/Swing-based application internals. 
 * 	 Copyright (C) 2012, Maxim Zakharenkov
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *   
 */
package org.swingexplorer;

import java.awt.Point;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
 *
 * @author  Maxim Zakharenkov
 */
public class PNLStatusBar extends javax.swing.JPanel {
    
    /** Creates new form PNLStatusBar */
    public PNLStatusBar() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtSection0 = new javax.swing.JTextField();
        txtSection1 = new javax.swing.JTextField();
        txtSection2 = new javax.swing.JTextField();

        txtSection0.setEditable(false);
        txtSection0.setToolTipText("Current component's containment path");

        txtSection1.setColumns(8);
        txtSection1.setEditable(false);
        txtSection1.setToolTipText("Relative mouse coordinates");

        txtSection2.setColumns(8);
        txtSection2.setEditable(false);
        txtSection2.setToolTipText("Absolute mouse coordinates");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(txtSection0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(txtSection1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(txtSection2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                .add(txtSection2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(txtSection1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(txtSection0, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField txtSection0;
    private javax.swing.JTextField txtSection1;
    private javax.swing.JTextField txtSection2;
    // End of variables declaration//GEN-END:variables
    
    MdlSwingExplorer model;
    ModelListener modelListener = new ModelListener();
    
    public void setModel(MdlSwingExplorer model) {

		if (model == this.model) {
			return;
		}

		if (this.model != null) {
			this.model.removePropertyChangeListener(modelListener);
		}
		if (model != null) {
			model.addPropertyChangeListener(modelListener);
		}
		this.model = model;
		repaint();
   }
    
    class ModelListener implements PropertyChangeListener {

		public void propertyChange(PropertyChangeEvent evt) {
			String propName = evt.getPropertyName();
			if ("mouseLocation".equals(propName)) {
				Point loc = model.getMouseLocation();
				Point absLoc = model.getMouseLocationAbsolute();
				if (loc != null) {
					setText(1, loc.x + "," + loc.y);
					setText(2, absLoc.x + "," + absLoc.y);
				} else {
					setText(1, "");
					setText(2, "");
				}
			}
			if ("statusText".equals(propName)) {
				setText(0, model.getStatustext());

			}
		}
	}

	
    
    public void setText(int section, String text) {
        switch(section) {
            case 0:
                txtSection0.setText(text);
                break;
            case 1:
                txtSection1.setText(text);
            case 2:
                txtSection2.setText(text);
        }
    }
}

